<template>
  <div class="table-box">
    <div class="table-search">
      <div class="btn-box">
        <el-button type="primary" plain @click="onAdd">新增</el-button>
      </div>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column label="图片" align="center">
            <template #default="scope">
              <el-image style="width: 70px; height: 70px" :src="scope.row.img" alt="" fit="fill"
                :preview-src-list="[scope.row.img]" :preview-teleported="true"></el-image>
            </template>
          </el-table-column>
          <el-table-column prop="url" label="链接" align="center" />
          <el-table-column prop="start_time" label="开始时间" align="center" :formatter="formatterTime" width="180" />
          <el-table-column prop="end_time" label="结束时间" align="center" :formatter="formatterTime" width="180" />
        </el-table>
        <div class="table-pagination">
          <el-pagination :current-page="pageable.pageNum" :page-size="pageable.pageSize" :page-sizes="[10, 25, 50, 100]"
            :background="true" layout="total, sizes, prev, pager, next, jumper" :total="pageable.total"
            @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm @cancel="cancel" @closeDialog="closeDialog" @submitForm="submit"
      @handleUploaderImgRequest="handleUploader" :dialogFormVisible="dialogVisible" :dialogForm="dialogForm"
      :btnLoading="btnLoading" />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime } from "@/utils/utils";
import { activityListApi, activityAddApi } from "@/api/moby/activity/activity";
import DialogForm from "./components/dialogForm.vue";
const dialogVisible = ref(false);
const tableData = ref([]);
const loading = ref(false);
const btnLoading = ref(false);
// 分页
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
const dialogFormData = {
  img: "",
  url: "",
  timeData: [],
};
const dialogForm = reactive({ ...dialogFormData });
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
  };
};

const onAdd = () => {
  dialogVisible.value = true;
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;

  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogVisible.value = false;

  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async (param) => {
  try {
    btnLoading.value = true;
    const res = await activityAddApi(param);

    ElMessage({
      message: res.message,
      type: "success",
    });

    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};
// //上传图标
const handleUploader = (res) => {
  if (res.code === 0) {
    ElMessage({
      message: "图片上传成功",
      type: "success",
    });
    dialogForm.img = res.data.name || "";
  }
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await activityListApi(data);

  let head = {
    img: "图片",
    url: "链接",
    start_time: "开始时间",
    end_time: "结束时间",
  };
  exportToExcel(res.data.results, head, "Mobybox-活动列表");
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await activityListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};

// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>

<style lang="scss" scoped></style>
